<?php

namespace App\Http\Controllers\Admin;

use App\Repository\Admin\AdminUsersRepository;
use App\Repository\Commit\PositionRepository;
use App\Repository\Commit\RoleRepository;
use App\Repository\SystemRepository;
use App\Repository\UploadRepository;
use Illuminate\Http\Request;

// use SebastianBergmann\CodeCoverage\Report\Xml\Report;

/**
 * @OA\Tag(
 *     name="后台菜单模块",
 *     description="菜单模块",
 * )
 */
class AdminUsersController extends BaseController
{
    public $repoist;
    public $system;
    public $role;
    public $position;

    public function __construct(AdminUsersRepository $AdminUsersRepository, SystemRepository $SystemRepository, RoleRepository $RoleRepository, PositionRepository $PositionRepository)
    {
        $this->repoist = $AdminUsersRepository;
        $this->system = $SystemRepository;
        $this->role = $RoleRepository;
        $this->position = $PositionRepository;
    }

    public function index(Request $Request)
    {
        $data = $this->repoist->index($Request);
        return $data;
    }

    public function updateOnline(Request $request)
    {
        return $this->repoist->updateOnline($request->all());
    }

    public function noInList(Request $Request)
    {
        $data = $this->repoist->noInList($Request);
        return $data;
    }

    public function onSel()
    {
    }

    public function store(Request $Request)
    {
        //用户添加
        $allData = $Request->all();

        $allData = lowerFitstArr($allData);
        return $this->repoist->store($allData);
    }

    public function edit(Request $Request)
    {
        $allData = $Request->all();
        $allData = lowerFitstArr($allData);
//        print_r($allData);
        $where = [
            'id' => $allData['id'],
        ];
        return $this->repoist->edit($where, $allData);
    }

    public function del(Request $Request, $id)
    {
        $where = [
            'id' => $id,
//            'shop_id' => $Request->user->shop_id,
        ];
        if (intval($Request->user->role_id) !== 1) {
            $where['shop_id'] = $Request->user->shop_id;
        }
        return $this->repoist->del($where);
    }

    public function getUser(Request $Request)
    {
        $where = [];
        if ($Request->user->role_id != 1) {
            $where = [
                'shop_id' => $Request->user->shop_id,
                'status' => 0
            ];
        }

        // 返回数据
        $return = [
            'code' => 200,
            'msg' => '操作成功',
            'roles' => $this->role->getAllData($where), // 角色
            'positions' => $this->position->getAllData($where), // 部门
        ];
        $msg = '操作成功';
        if (empty($return['roles'])) {
            $return['code'] = -1;
            $msg = '未查询到角色 请前去添加角色';
        }
        // if (empty($return['positions'])) {
        //   $return['code'] = -1;
        //   $msg = '未查询到部门 请前去添加部门';
        // }
        $return['msg'] = $msg;
        return $return;
    }

    public function getOneUser(Request $Request, $id)
    {
        if (empty($id)) {
            return $this->returnMsg(1, '请传递标识');
        }
        $where = [
            'id' => $id
        ];

        // 返回数据
        $datas = $this->repoist->oneSel($where);
        $pots = [];
        if (!empty($datas['positionId'])) {
            $resPosit = explode('.', $datas['positionId']);
            foreach ($resPosit as $k => $v) {
                $pots[] = (int)($v);
            }
            unset($datas['positionId']);
        }
        $roles = [];
        if (!empty($datas['roleId'])) {
            $resRol = explode('.', $datas['roleId']);
            foreach ($resRol as $k => $v) {
                $roles[] = (int)($v);
            }
            unset($datas['roleId']);
        }
        $whereRole = [
            'status' => 0
        ];
        if ($Request->user->role_id != 1) {
            $whereRole['shop_id'] = $Request->user->shop_id;
        }
        $return = [
            'code' => 200,
            'msg' => '操作成功',
            'data' => $datas,
            'postIds' => $pots,
            'roleIds' => $roles,
            'roles' => $this->role->getAllData($whereRole), // 角色
            'positions' => $this->position->getAllData($whereRole), // 部门
        ];
        $msg = '操作成功';
        if (empty($return['roles'])) {
            $return['code'] = -1;
            $msg = '未查询到角色 请前去添加角色';
        }
        if (empty($return['positions'])) {
            // $return['code'] = -1;
            // $msg = '未查询到部门 请前去添加部门';
        }
        $return['msg'] = $msg;
        return $return;
    }

//    新增会员与修改会员时的主管人员列表
    public function supervisorOptionsList(Request $request)
    {
        return $this->repoist->supervisorOptionsList($request->all());
    }

    public function changeStatus(Request $Request)
    {
        $id = (int)$Request->get('userId');
        $status = (int)$Request->get('status');

        return $this->repoist->changeStatus($id, $status);
    }

//重置密码
    public function userResetPwd(Request $Request)
    {
        $where = [
            'id' => $Request->get('userId')
        ];
        $update = [
            'password' => $Request->get('password'),

        ];
        return $this->repoist->edit($where, $update);
    }

    public function resetUser(Request $Request)
    {
        return $this->repoist->resetUser($Request->all());
    }

//    //重置导出密码
    public function userResetExpPwd(Request $Request)
    {
        $where = [
            'id' => $Request->get('userId')
        ];
        $update = [
            'exp_password' => $Request->get('exp_password'),

        ];
        return $this->repoist->edit($where, $update);
    }

    //查询所有的权限
    public function userAuthRole(Request $Request, $id)
    {
        $where = [
            'id' => $id,
        ];
        $data = $this->repoist->oneSelRole($where);
        return [
            'code' => 200,
            'msg' => '查询成功',
            'data' => $data,
            'roles' => $this->role->getAllData()
        ];
    }

    // 个人中心的数据
    public function userProfile(Request $Request)
    {
        return [
            'code' => 200,
            'msg' => '查询成功',
            'data' => $Request->user
        ];
        // var_dump($Request->user, 333333333);
    }

    // 个人数据 修改
    public function userEditProfile(Request $Request)
    {
        $where = [
            'id' => $Request->user->id
        ];
        $requerys = [
            'nick_name' => $Request->get('nick_name'),
            'phonenumber' => $Request->get('phonenumber'),
            'email' => $Request->get('email'),
            'sex' => $Request->get('sex'),
        ];
        // var_dump($requerys,11111111111);
        return $this->repoist->edit($where, $requerys);
    }

    // 用户修改密码
    public function updatePwd(Request $Request)
    {
        $where = [
            'id' => $Request->user->id
        ];
        if (!empty($Request->get('newPassword')) && !empty($Request->get('oldPassword'))) {
            $requerys = [
                'password' => $Request->get('newPassword'),
                'oldPassword' => $Request->get('oldPassword')
            ];
        }
        if (!empty($Request->get('oldExpPassword')) && !empty($Request->get('newExpPassword'))) {
            $requerys = [
                'exp_password' => $Request->get('newExpPassword'),
                'oldExpPassword' => $Request->get('oldExpPassword')
            ];
        }
        if (empty($requerys)) {
            return $this->returnMsg(103, '请刷新页面重新修改');
        }

        return $this->repoist->edit($where, $requerys);
    }

    // 用户头像上传\
    public function userProfileAvatar(Request $Request)
    {
        $model = new UploadRepository();
        $res = $model->upload($Request, 'avatarfile');
        if (empty($res)) {
            return [
                'code' => 100,
                'msg' => '上传失败',

            ];
        }
        $where = [
            'id' => $Request->user->id
        ];
        $resData = $this->repoist->avatar($where, $res['avatarfile_src']);
        $resData['imgUrl'] = $res['avatarfile_src'];
        return $resData;
    }

}
